//
//  KSPullZhiBiaoView.swift
//  ZeroShare
//
//  Created by July on 2020/3/28.
//  Copyright © 2020 saeipi. All rights reserved.
//

import UIKit

class KSPullZhiBiaoView: UIView {
    var strokeColor: UIColor?
    var fillColor: UIColor?
    var kitRadius: CGFloat        = 5.0
    var lineWidth: CGFloat        = 1.0
    var triangleCenterX: CGFloat = 0.0
    var isTop: Bool              = true
    var bottomArray = [String]()
    var topArray = [String]()
    var bottomBlock:((String)->Void)?
    var topBlock:((String)->Void)?
    
    var selectTopStr = "MA"{
        willSet{
            for i in 0..<self.topArray.count{
                let button = self.viewWithTag(10+i) as? UIButton
                if topArray[i] == newValue {
                    button?.setTitleColor(UIColor.yellow, for: .normal)
                }else{
                    button?.setTitleColor(UIColor.gray, for: .normal)
                }
            }
            if newValue == "nil" {
                let button = self.viewWithTag(99) as? UIButton
                button?.setImage(UIImage.init(named: "ic_chart_closed_eyes"), for: .normal)
            }
        }
    }
    var selectBottomStr = "MACD"{
        willSet{
            for i in 0..<self.bottomArray.count{
                let button = self.viewWithTag(20+i) as? UIButton
                if bottomArray[i] == newValue {
                    button?.setTitleColor(UIColor.yellow, for: .normal)
                }else{
                    button?.setTitleColor(UIColor.gray, for: .normal)
                }
            }
            if newValue == "nil" {
                let button = self.viewWithTag(100) as? UIButton
                button?.setImage(UIImage.init(named: "ic_chart_closed_eyes"), for: .normal)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        createUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func createUI(){
        let topArray = ["MA","EMA","BOOL"]
        self.topArray = topArray
        for (index,str) in topArray.enumerated(){
            let button = UIButton.init(frame: CGRect.init(x: CGFloat(index)*70, y: 0, width: 70, height: 50))
            button.setTitle(str, for: .normal)
            button.setTitleColor(UIColor.gray, for: .normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            button.tag = 10+index
            button.addTarget(self, action: #selector(onTopClick(_:)), for: .touchUpInside)
            self.addSubview(button)
            if selectTopStr == str {
                button.setTitleColor(UIColor.yellow, for: .normal)
            }
        }
        
        let button1 = UIButton.init(frame: CGRect.init(x: UIScreen.main.bounds.size.width-60-32, y: 0, width: 60, height: 50))
        button1.setImage(UIImage.init(named: "ic_chart_open_eyes"), for: .normal)
        button1.tag = 99
        button1.addTarget(self, action: #selector(onTopYanClick), for: .touchUpInside)
        self.addSubview(button1)
        
        let bottomArray = ["MACD","KDJ","RSI","BOOL"]
        self.bottomArray = bottomArray
        for (index,str) in bottomArray.enumerated(){
            let button = UIButton.init(frame: CGRect.init(x: CGFloat(index)*70, y: 50, width: 70, height: 50))
            button.setTitle(str, for: .normal)
            button.setTitleColor(UIColor.gray, for: .normal)
            button.tag = 20+index
            button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            button.addTarget(self, action: #selector(onBottomClick(_:)), for: .touchUpInside)
            self.addSubview(button)
            if selectBottomStr == str {
                button.setTitleColor(UIColor.yellow, for: .normal)
            }
        }
        
        let button2 = UIButton.init(frame: CGRect.init(x: UIScreen.main.bounds.size.width-60-32, y: 50, width: 60, height: 50))
        button2.tag = 100
        button2.setImage(UIImage.init(named: "ic_chart_open_eyes"), for: .normal)
        button2.addTarget(self, action: #selector(onBottomYanClick), for: .touchUpInside)
        self.addSubview(button2)
    }
    
    @objc func onTopYanClick(){
        for i in 0..<self.topArray.count{
            let button = self.viewWithTag(10+i) as? UIButton
            button?.setTitleColor(UIColor.gray, for: .normal)
        }
        if self.topBlock != nil {
            self.topBlock!("nil")
        }
    }
    
    @objc func onBottomYanClick(){
        for i in 0..<self.bottomArray.count{
            let button = self.viewWithTag(20+i) as? UIButton
            button?.setTitleColor(UIColor.gray, for: .normal)
        }
        if self.bottomBlock != nil {
            self.bottomBlock!("nil")
        }
    }
    
    @objc func onTopClick(_ button:UIButton){
        if self.topBlock != nil {
            self.topBlock!(button.currentTitle!)
        }
    }
    
    @objc func onBottomClick(_ button:UIButton){
        if self.bottomBlock != nil {
            self.bottomBlock!(button.currentTitle!)
        }
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    func drawBubble() {
        let kitWidth:CGFloat          = self.frame.width
        let kitHeight:CGFloat         = self.frame.height
        let angleBottomWidth: CGFloat = 12.0
        let triangleHeight: CGFloat   = 5
        
        let triangleBottomY           = isTop ? (lineWidth + triangleHeight) : (kitHeight - lineWidth - triangleHeight)
        let cornerY                   = isTop ? lineWidth : (kitHeight - lineWidth)

        let leftTopX                  = kitRadius + lineWidth
        let leftTopY                  = isTop ? triangleBottomY : lineWidth

        let leftTopArcX               = leftTopX
        let leftTopArcY               = leftTopY + kitRadius

        let rightTopArcX              = kitWidth - lineWidth - kitRadius
        let rightTopArcY              = leftTopArcY

        let rightBottomArcX           = rightTopArcX
        let rightBottomArcY           = isTop ? (kitHeight - kitRadius) : (kitHeight - lineWidth - triangleHeight - kitRadius)

        let leftBottomArcY            = rightBottomArcY
        let leftBottomArcX            = leftTopX

        let bezierPath                = UIBezierPath.init()
        //起始点
        bezierPath.move(to: CGPoint(x: leftTopX, y: leftTopY))
        //顶部三角
        if isTop {
            bezierPath.addLine(to: CGPoint(x: 100 - angleBottomWidth / 2, y: triangleBottomY))
            bezierPath.addLine(to: CGPoint(x: 100, y: cornerY))
            bezierPath.addLine(to: CGPoint(x: 100 + angleBottomWidth / 2, y: triangleBottomY))
        }
        //右上角圆弧
        bezierPath.addArc(withCenter: CGPoint(x: rightTopArcX, y: rightTopArcY), radius: kitRadius, startAngle: -CGFloat(Double.pi / 2), endAngle: 0.0, clockwise: true)
        //右下角圆弧
        bezierPath.addArc(withCenter: CGPoint(x: rightBottomArcX, y: rightBottomArcY), radius: kitRadius, startAngle: 0.0, endAngle: CGFloat(Double.pi / 2), clockwise: true)
        if isTop == false {
            bezierPath.addLine(to: CGPoint(x: triangleCenterX + angleBottomWidth / 2, y: triangleBottomY))
            bezierPath.addLine(to: CGPoint(x: triangleCenterX, y: cornerY))
            bezierPath.addLine(to: CGPoint(x: triangleCenterX - angleBottomWidth / 2, y: triangleBottomY))
        }
        //左下角圆弧
        bezierPath.addArc(withCenter: CGPoint(x: leftBottomArcX, y: leftBottomArcY), radius: kitRadius, startAngle: CGFloat(Double.pi / 2), endAngle: CGFloat(Double.pi), clockwise: true)
        //左上角圆弧
        bezierPath.addArc(withCenter: CGPoint(x: leftTopArcX, y: leftTopArcY), radius: kitRadius, startAngle: CGFloat(Double.pi), endAngle: CGFloat(Double.pi * 1.5), clockwise: true)

        let maskLayer                 = CAShapeLayer()
        maskLayer.frame               = CGRect(x: 0, y: 0, width: kitWidth, height: kitHeight)

        let borderLayer               = CAShapeLayer()
        borderLayer.frame             = CGRect(x: 0, y: 0, width: kitWidth, height: kitHeight)
        borderLayer.lineWidth         = lineWidth
        borderLayer.strokeColor       = strokeColor?.cgColor
        borderLayer.fillColor         = fillColor?.cgColor

        maskLayer.path                = bezierPath.cgPath
        borderLayer.path              = bezierPath.cgPath

        self.layer.insertSublayer(borderLayer, at: 0)
        self.layer.mask              = maskLayer
    }

}
